package tmproject.hlhj.fhp.cartrans.activitys

import android.animation.ObjectAnimator
import android.app.Activity
import android.content.Intent
import android.view.View
import com.bumptech.glide.Glide
import com.huantansheng.easyphotos.EasyPhotos
import kotlinx.android.synthetic.main.aty_regis.*
import tmproject.hlhj.fhp.cartrans.IView.RegisView
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.presenter.RegisPresenter
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.customVIews.ChooseNumPop
import tmproject.hlhj.fhp.cartrans.customVIews.MyGlideEngin
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils
import java.util.*
import tmproject.hlhj.fhp.cartrans.MainActivity
import android.app.DatePickerDialog
import android.text.InputFilter
import android.view.WindowManager
import android.widget.DatePicker
import tmproject.hlhj.fhp.cartrans.javaBean.CarNumBean
import tmproject.hlhj.fhp.cartrans.javaBean.RegisBean
import tmproject.hlhj.fhp.cartrans.utils.FormatUtil
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import java.io.File
import kotlin.collections.ArrayList


/**
 * Created by Administrator on 2018/8/11/011.
 */
class RegisAty : BaseAty<RegisView, RegisPresenter>(), RegisView {
    private val IDF=11
    private val IDB=12
    private val ZGB=13
    private val ZGF=14

    private var idf=""
    private var idb=""
    private var zgf=""//驾驶证
    private var zgb=""//资格证
    private var sex="m"
    private var chooseNumPop:ChooseNumPop?=null
    private var carNumId:Int?=0
    private var carNum:String?=""
    override fun bindView(): RegisView {
        return this
    }


    override fun createPresenter(): RegisPresenter {
        return RegisPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.aty_regis
    }

    private var mYear: Int=0

    private var mMonth: Int=0

    private var mDay: Int=0

    override fun initView() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        val ca = Calendar.getInstance()
        mYear = ca.get(Calendar.YEAR)
        mMonth = ca.get(Calendar.MONTH)
        mDay = ca.get(Calendar.DAY_OF_MONTH)

        etPhone.setText(LoginAty.phoneStr)
        etName.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(5))
        etPhone.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
        etAge.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(2))

    }

    override fun regisResp(regisBean: RegisBean) {
        ToastUtils.toast("注册成功")
        SpHelper.saveToken(this,regisBean.data.token)
        startActivity(Intent(this@RegisAty,HomeAty::class.java))
        finish()
    }

    override fun carNumResp(carNumBean: CarNumBean) {
        if (chooseNumPop==null){
            log("fhp",loNum.width.toString())
            chooseNumPop=ChooseNumPop(this@RegisAty,loNum.width)
        }
        chooseNumPop?.let {
            it.setData(carNumBean.data as ArrayList<CarNumBean.DataBean>)
            it.listener=object :ChooseNumPop.OnNumSelectListener{
                override fun selectNum(id: Int, num: String) {
                    carNumId=id
                    etCarNum.text=num
                }
            }
        }
    }

    override fun logicStart() {
        presenter?.getCarNum(this)//获取车牌号列表
    }

    override fun setListener() {
        btExit.setOnClickListener {
            finish()
        }
        btDone.setOnClickListener {
            if (etAvTime.text.isEmpty()){
                ToastUtils.toast("驾驶证有效期未填写")
                return@setOnClickListener
            }
            if (etZg.text.isEmpty()){
                ToastUtils.toast("驾驶证有效期未填写")
                return@setOnClickListener
            }
            if (etCarNum.text.isEmpty()){
                ToastUtils.toast("车牌号未填写")
                return@setOnClickListener
            }
            if (zgf.isEmpty()||zgb.isEmpty()){
                ToastUtils.toast("证件未上传")
                return@setOnClickListener
            }

            presenter?.regis(this@RegisAty,etName.text.toString(),etPhone.text.toString(),sex,etAge.text.toString().toInt(),
                    etNum.text.toString(),etAvTime.text.toString(),etZg.text.toString(),carNumId.toString(),
                    File(idf), File(idb),File(zgf),File(zgb))
        }
        etZg.setOnClickListener {
            DatePickerDialog(this@RegisAty, object :DatePickerDialog.OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val days: String
                    if (month + 1 < 10) {
                        if (dayOfMonth < 10) {
                            days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                        } else {
                            days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                        }

                    } else {
                        if (dayOfMonth < 10) {
                            days = StringBuffer().append(year).append("-").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                        } else {
                            days = StringBuffer().append(year).append("-").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                        }

                    }
                    etZg.setText(days)

                }
            }, mYear, mMonth, mDay).show()
        }
        etAvTime.setOnClickListener {
            DatePickerDialog(this@RegisAty, object :DatePickerDialog.OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val days: String
                    if (month + 1 < 10) {
                        if (dayOfMonth < 10) {
                            days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                        } else {
                            days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                        }

                    } else {
                        if (dayOfMonth < 10) {
                            days = StringBuffer().append(year).append("-").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                        } else {
                            days = StringBuffer().append(year).append("-").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                        }

                    }
                    etAvTime.setText(days)

                }
            }, mYear, mMonth, mDay).show()
        }
        rbSex.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rbF->{
                    sex="F"
                }
                R.id.rbM->{
                    sex="M"
                }
            }
        }
        btNext.setOnClickListener {
            if (etName.text.isEmpty()){
                ToastUtils.toast("姓名未填写")
                return@setOnClickListener
            }
            if (etPhone.text.isEmpty()){
                ToastUtils.toast("手机号")
                return@setOnClickListener
            }
            if (sex.equals("m")){
                ToastUtils.toast("性别未选择")
                return@setOnClickListener
            }
            if (etAge.text.isEmpty()){
                ToastUtils.toast("年龄未填写")
                return@setOnClickListener
            }
            if (!FormatUtil.isIDNumber(etNum.text.toString())){
                ToastUtils.toast("身份证号格式不正确")
                return@setOnClickListener
            }
            if (idf.isEmpty()||idb.isEmpty()){
                ToastUtils.toast("图片信息未完整")
                return@setOnClickListener
            }

            val animator1 = ObjectAnimator.ofFloat(scrollView1, "translationX", 0f, -2000f)
            animator1.duration=800
            animator1.start()
            val animator2 = ObjectAnimator.ofFloat(scrollView2, "translationX", 2000f, 0f)
            animator2.duration=800
            animator2.start()
        }

        btSearch.setOnClickListener {
            chooseNumPop?.let {
                if (!it.isShowing){
                    imgSearch.setImageResource(R.mipmap.ic_shang)
                    it.showAsDropDown(loNum)
                }else{
                    imgSearch.setImageResource(R.mipmap.ic_xia)
                    it.dismiss()
                }
            }
        }

        idF.setOnClickListener {
            EasyPhotos.createAlbum(this@RegisAty, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(IDF);
        }
        idB.setOnClickListener {
            EasyPhotos.createAlbum(this@RegisAty, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(IDB);
        }
        zgF.setOnClickListener {
            EasyPhotos.createAlbum(this@RegisAty, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(ZGF);
        }
        zgB.setOnClickListener {
            EasyPhotos.createAlbum(this@RegisAty, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(ZGB);
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            IDF->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                btFront.visibility=View.GONE
                idf=pathList[0]
                Glide.with(this@RegisAty).load(idf).into(idF)
            }
            IDB->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                btBack.visibility=View.GONE
                idb=pathList[0]
                Glide.with(this@RegisAty).load(idb).into(idB)
            }
            ZGF->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                btZgFront.visibility=View.GONE
                zgf=pathList[0]
                Glide.with(this@RegisAty).load(zgf).into(zgF)

            }
            ZGB->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                btZgBack.visibility=View.GONE
                zgb=pathList[0]
                Glide.with(this@RegisAty).load(zgb).into(zgB)
            }
        }
    }
}